package Api;

import Util.HttpUtil;
import Util.MyKey;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import static Util.Tools.*;

/**
 * 收款凭证Api
 */

public class TradeQueryAPIV2ReceiptApi {
    public static void main(String[] args) {
        try {
            // 目标URL
            String url = "https://pay.heepay.com/Payment/TradeQueryAPIV2Receipt.aspx";
            //需要的MD5密钥
            String key = MyKey.MD5Key1;
            //商户编号，（汇付宝商户内码：七位整数数字）
            String agent_id = "1664502";
            // 获取当前14位时间戳
            Date now = new Date();
            // 创建 SimpleDateFormat 对象，指定日期格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String timestamp = dateFormat.format(now);
            //固定版本号
            String version = "2";
            //商户订单号
            //String agent_bill_id = "AGPAY20221108150835242626";
            String bill_no = "H2403194791421AQ";

            String sign_type = "MD5";
            String charset = "utf-8";

            JSONObject biz_content = new JSONObject();
            //biz_content.put("agent_bill_id", agent_bill_id);
            biz_content.put("bill_no", bill_no);



            // 请求参数，以Map形式表示
            Map<String, String> parameters = new HashMap<>();
            //拼接待加密参数
            String Date =
                    "agent_id=" +
                    agent_id +
                    "&biz_content=" +
                    biz_content +
                    "&charset=" +
                    charset +
                    "&sign_type=" +
                    sign_type +
                    "&timestamp=" +
                    timestamp +
                    "&version=" +
                    version +
                    "&key=" +
                    key;
            System.out.println("待签名串：" + Date);
            System.out.println();

            //MD5加密得出签名结果
            String sign  = getMD5(Date);

            assert sign != null;
            String sign1 = sign.toUpperCase(Locale.forLanguageTag(sign));


            parameters.put("version", version);
            parameters.put("agent_id", agent_id);
            parameters.put("biz_content", String.valueOf(biz_content));
            parameters.put("charset", charset);
            parameters.put("sign_type", sign_type);
            parameters.put("timestamp", timestamp);
            parameters.put("sign", sign1);

            //打印请求参数
            String Str = MaptoStr(parameters);
            System.out.println("发送请求数据："+ Str);
            System.out.println();

            String response = HttpUtil.sendPost2(url, parameters);
            System.out.println("请求返回参数：--------->" + response);
            System.out.println();

            //解析返回参数
            JsonNode jsonNode = parseJsonResponse(response);
            String return_code = jsonNode.get("return_code").asText();

            if ("SUCCESS".equalsIgnoreCase(return_code)) {
                String file_path = jsonNode.get("file_path").asText();
                System.out.println("输出凭证下载地址：--->"+file_path);
            }else {
                String return_msg = jsonNode.get("return_msg").asText();
                System.out.println("输出失败return_msg：--------->"+return_msg);
            }


        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

